const app = getApp()
import Dialog from '../../vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    show: false,
    onVip:false,
    overlay:false,
    coupon_show:false,
    coupon_aumont:0,
    ucode:'',
    ucode_user:{
      face:'/img/me.png',
    },
    page:0,
    config:[],
    vip: {isvip: 0,is_up: 1,num:0,card:{},icard:{}},
    coupon: [],
  },
   //生命周期函数--监听页面加载
   onLoad: function (options){
    app.getParam(options,(param)=>{
      if(!app.util().isNull(param.ucode)){
        this.setData({
          ucode:param.ucode
        })
      }
    });
  },
  //每次显示重新载入
  onShow: function () {
    app.isLogin(() => {
      this.getVip();
    });
  },
  //监听页面准备好
  onReady: function () {
    this.setData({
      config: app.globalData.appConfig
    })
  },
  //底部加载更多
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.couponApi();
  },
  //获取会员卡信息
  getVip:function(){
    var param = {
      signkey: app.util().getRandom(12),
    }
    app.api().Get('api/v4/allwin/vip/isvip',param,(rel) => {
      if (rel.code == 200){
        this.setData({
          vip:rel.data,
          onVip:true,
        })
        if(rel.data.is_up && app.util().isNull(this.data.coupon)){
          this.couponApi();
        }
      }
      this.setData({
        loading: false
      })
    })
  },
  //刷新
  couponApi: function () {
    if (this.data.loading) {
      var param = {
        vipid:this.data.vip.card.id,
        page:this.data.page+1
      }
      app.api().Get('api/v4/allwin/vip/coupon',param,(result)=>{
        if (result.code == 200) {
          var coupon = this.data.coupon;
          for (let i in result.data) {
            coupon.push(result.data[i]);
          }
          this.setData({
            coupon: coupon,
            page: param.page,
          });
        }
      })
      this.setData({
        loading: false
      })
    }
  },
  //支付开通
  doPayment:function (e) {
    wx.showLoading({title:'加载中',mask: true });
    var parms = {
      signkey: app.util().getRandom(12),
      vipcard: this.data.vip.card.id,
      ucode: app.globalData.ucode
    }
    app.api().Post('api/v4/allwin/vip/payment',parms,(rel) => {
      if(rel.data.isvip == 1){
        if(rel.data.coupon_aumont){
          this.setData({
            coupon_show:true,
            coupon_aumont:rel.data.coupon_aumont,
          })
        }else{
          app.wxAlert(rel.msg,() => {
            this.getVip();
          })
        }
      }else{
        app.doWechatPay(rel.data.pay,(res)=>{
          this.getVip();
        },(res) => {
           app.wxAlert('取消支付或开通失败')
        });
      }
      wx.hideLoading();
    })
  },
//获取邀请用户
  getUcode:function(){
    if (!app.util().isNull(app.globalData.ucode)){
      app.api().Get('openapi/v1/user/getCodeUser', {'ucode': app.globalData.ucode},(rel)=>{
        if (rel.code == 200) {
          this.setData({
            ucode: app.globalData.ucode,
            ucode_user: rel.data,
          })
        }
      })
    }
  },
  //输入邀请码
  inputCode: function (event){
    var cursor = event.detail.cursor, code = event.detail.value;
    if (cursor >= 4){
      app.globalData.ucode = code;
      this.getUcode();
    }else{
      this.setData({
        ucode_user: {
          face: '/img/me.png'
        }
      })
    }
  },
  //关闭弹出窗口
  onClose:function(){
    this.setData({
      show: !this.data.show
    })
    if(this.data.show){
      this.getUcode();
    }
  },
  //会员二维码
  qrcode: function (e) {
    wx.navigateTo({
      url: '../user/qrcode',
    })
  },
  //查看店铺
  onStore: function (e) {
    if(this.data.vip.isvip){
      if(this.data.vip.icard.store_id > 0){
        wx.navigateTo({
          url: '../store/views?store_id='+this.data.vip.icard.store_id,
        })
      }
    }
  },
  //会员制度
  onRules: function (e) {
    var config = wx.getStorageSync('config');
    wx.navigateTo({
      url: '/pages/helper/webview?src=' + app.apiroot + '/app-' + config.app_id + '/allwin/article/sla&ids=656'
    })
  },
  //分享按钮
  onShareAppMessage: function (res) {
    return {
      title: '我是'+app.appname+'的会员,'+ this.data.config.shore_text,
      imageUrl: this.data.config.shore_img,
      path: '/pages/card/vip?ucode='+app.globalData.loginuser.ucode
    }
  },
  //查看赠品
  onCoupon: function (){
    this.onCouponHide();
    wx.navigateTo({
      url: '../user/coupon',
    })
  },
  onClickHide() {
    this.setData({
      overlay: !this.data.overlay
    });
  },
  onCouponHide(){
    this.getVip();
    this.setData({
      coupon_show: !this.data.coupon_show
    });
  },
})